package com.px.system.service.impl;

import java.util.List;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.RandomUtil;
import com.px.common.constant.Constants;
import com.px.common.constant.UserConstants;
import com.px.common.core.domain.model.RegisterFlightUser;
import com.px.common.core.redis.RedisCache;
import com.px.common.utils.DateUtils;
import com.px.common.utils.SecurityUtils;
import com.px.common.utils.StringUtils;
import com.px.system.domain.vo.FlightUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.px.system.mapper.FlightUserMapper;
import com.px.system.domain.FlightUser;
import com.px.system.service.IFlightUserService;

/**
 * 飞行用户信息Service业务层处理
 * 
 * @author WRJ
 * @date 2024-08
 */
@Service
public class FlightUserServiceImpl implements IFlightUserService 
{
    @Autowired
    private FlightUserMapper flightUserMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询飞行用户信息
     * 
     * @param userId 飞行用户信息主键
     * @return 飞行用户信息
     */
    @Override
    public FlightUser selectFlightUserByUserId(Long userId)
    {
        return flightUserMapper.selectFlightUserByUserId(userId);
    }

    /**
     * 查询飞行用户信息列表
     * 
     * @param flightUser 飞行用户信息
     * @return 飞行用户信息
     */
    @Override
    public List<FlightUser> selectFlightUserList(FlightUser flightUser)
    {
        return flightUserMapper.selectFlightUserList(flightUser);
    }

    /**
     * 新增飞行用户信息
     * 
     * @param flightUser 飞行用户信息
     * @return 结果
     */
    @Override
    public int insertFlightUser(FlightUser flightUser)
    {
        return flightUserMapper.insertFlightUser(flightUser);
    }

    /**
     * 修改飞行用户信息
     * 
     * @param flightUser 飞行用户信息
     * @return 结果
     */
    @Override
    public int updateFlightUser(FlightUser flightUser)
    {
        return flightUserMapper.updateFlightUser(flightUser);
    }

    /**
     * 批量删除飞行用户信息
     * 
     * @param userIds 需要删除的飞行用户信息主键
     * @return 结果
     */
    @Override
    public int deleteFlightUserByUserIds(Long[] userIds)
    {
        return flightUserMapper.deleteFlightUserByUserIds(userIds);
    }

    /**
     * 删除飞行用户信息信息
     * 
     * @param userId 飞行用户信息主键
     * @return 结果
     */
    @Override
    public int deleteFlightUserByUserId(Long userId)
    {
        return flightUserMapper.deleteFlightUserByUserId(userId);
    }

    @Override
    public String register(RegisterFlightUser user) {
        String msg = "";
        String password = user.getPassword();
        String phonenumber = user.getPhonenumber();

        //验证码失效
        if(StringUtils.isNull(redisCache.getCacheObject(phonenumber+Constants.REGISTER)))
            return "验证码失效!";

        //验证码错误
        if(!user.getCode().equals(redisCache.getCacheObject(phonenumber+Constants.REGISTER)))
            return "验证码错误!";


        //查询是否存在
        FlightUser info = flightUserMapper.checkUserName(phonenumber);
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
        {
            msg = "密码长度必须在5到20个字符之间";
        }
        else if (StringUtils.isNotNull(info))
        {
            msg = "注册失败，注册手机号"+phonenumber+"已存在";
        }
        else
        {
            FlightUser flightUser = Convert.convert(FlightUser.class, user);
            flightUser.setUserName(phonenumber);
            flightUser.setNickName("飞行用户" + RandomUtil.randomNumbers(4));
            flightUser.setPassword(SecurityUtils.encryptPassword(password));
            flightUser.setCreateTime(DateUtils.getNowDate());
            if (flightUserMapper.insertFlightUser(flightUser) < 1)
            {
                msg = "注册失败,请联系系统管理人员";
            }
        }
        return msg;
    }

    @Override
    public FlightUser selectUserByUserName(String username) {
        return flightUserMapper.selectUserByUserName(username);
    }

    @Override
    public boolean updateUserAvatar(Long userId, String avatar) {

        return flightUserMapper.updateUserAvatar(userId,avatar) > 0;
    }

    @Override
    public int resetUserPwd(Long userId, String password) {
        return flightUserMapper.resetUserPwd(userId,password);
    }

    @Override
    public List<FlightUserVo> selectFlightUserListMap(FlightUser flightUser) {
        return flightUserMapper.selectFlightUserListMap(flightUser);
    }

    @Override
    public String selectIdCardByPhone(String phoneNumber) {
        return flightUserMapper.selectIdCardByPhone(phoneNumber);
    }
}
